package com.epfresh.supplier.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderStoreDyn;
import com.epfresh.supplier.entity.OrderStoreDynStyle;
import com.epfresh.supplier.widget.CommonRecyclerAdapter;
import com.epfresh.supplier.widget.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import multitype.OnRecyclerViewItemClickListener;

public class HomePartBottomAdapter extends CommonRecyclerAdapter<OrderStoreDyn> implements View.OnClickListener {

    private Map<String, Integer> mapRes = new HashMap<>();
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public HomePartBottomAdapter(Context context, List<OrderStoreDyn> datas) {
        super(context, datas, R.layout.item_store_dyn);
        mapRes.clear();
        mapRes.put("ORDER", R.mipmap.icon_home_list_order);
        mapRes.put("JMT", R.mipmap.icon_home_list_halve);
        mapRes.put("COMMENT", R.mipmap.icon_home_list_comment);
        mapRes.put("PLATFORM", R.mipmap.icon_home_list_stand_book);
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public void setData(List<OrderStoreDyn> datas) {
        super.setData(datas);
    }


    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);
            if (onRecyclerViewItemClickListener != null && o instanceof OrderStoreDyn) {
                onRecyclerViewItemClickListener.onItemClick(v, position, o);
            }
        }
    }

    @Override
    public void convert(ViewHolder holder, final OrderStoreDyn post, int position) {
        holder.setText(R.id.tv_title, post.getTitle());
        holder.setText(R.id.tv_time, post.getCreateTime());
        ImageView icon = holder.getView(R.id.iv_icon);
        String type = post.getType();
        Integer res = null;
        if (type != null) {
            res = mapRes.get(type);
        }
        if (res != null) {
            icon.setBackgroundResource(res);
        } else {
            icon.setBackgroundResource(0);
        }
        List<OrderStoreDynStyle> textList = post.getTextList();
        if (textList != null) {
            StringBuilder sb = new StringBuilder();
            int k = 0;
            List<OrderStoreDynStyle> list = new ArrayList<>();
            for (int i = 0; i < textList.size(); i++) {
                OrderStoreDynStyle osd = textList.get(i);
                if (osd != null) {
                    String text = osd.getText();

                    if (text != null && text.length() > 0) {
                        if (k > 0) {
                            sb.append("\n\n");
                        }
                        list.add(osd);
                        k++;
                        sb.append(text);
                    }
                }
            }
            k = 0;
            int sl = 0;
            if (sb.length() > 0) {
                textList = list;
                SpannableStringBuilder sp = new SpannableStringBuilder(sb.toString());
                Log.e("textList111", "textList:" + textList);
                Log.e("textList111", "sb:" + sb);
                for (int i = 0; i < textList.size(); i++) {
                    OrderStoreDynStyle osd = textList.get(i);
                    if (osd != null) {
                        String text = osd.getText();
                        if (text != null && text.length() > 0) {
                            String style = osd.getStyle();
                            int s = 0;
                            int e = 0;
                            if (k > 0) {
                                sl += 2;
                                OrderStoreDynStyle osd1 = textList.get(i - 1);
                                String style1 = osd1.getStyle();
                                //相同5
                                if (style != null && style.equals(style1)) {
                                    sp.setSpan(new AbsoluteSizeSpan(0, true), sl - 1, sl, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小
                                } else {
                                    sp.setSpan(new AbsoluteSizeSpan(8, true), sl - 1, sl, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小
                                }
                            }
                            s = sl;
                            sl += text.length();
                            e = sl;

                            if ("1".equals(style)) {

                                sp.setSpan(new ForegroundColorSpan(0xFF434343), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
                                sp.setSpan(new AbsoluteSizeSpan(14, true), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小

                            } else if ("2".equals(style)) {
                                sp.setSpan(new ForegroundColorSpan(0xFF898989), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
                                sp.setSpan(new AbsoluteSizeSpan(10, true), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小
                            }
                            k++;

                        }
                    }
                }
                holder.setText(R.id.tv_content, sp);
            } else {
                holder.setText(R.id.tv_content, "");
            }

        } else {
            holder.setText(R.id.tv_content, "");
        }
        holder.itemView.setTag(R.id.item_key_position, holder.getAdapterPosition());
        holder.itemView.setTag(R.id.item_key_status, post);
        holder.itemView.setOnClickListener(this);
    }

}
